<?php
date_default_timezone_set('PRC');
header("Content-Type:text/html;charset=utf-8");

$phone = $_POST['user_phone'];
if(empty($phone)){
    echo json_encode(array('error'=>1,'msg'=>'请输入手机号码'));
    exit; //终止程序
}

// 发送验证码（云片网络-平台）
$apikey = "bde1940c698692d991401e47f4403bea"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
$mobile = $phone; //请用自己的手机号代替

/**/
$rand = mt_rand(100000,999999);
setcookie('code',$rand,time()+60);// 存储验证码
/**/

$text="【云片网】 您的验证码是".$rand;
$ch = curl_init(); // php扩展函数（模拟提交，异步请求）

/* 设置验证方式 */

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

/* 设置返回结果为流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* 设置超时时间*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

/* 设置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 取得用户信息
// $json_data = get_user($ch,$apikey);  //json 字符串
// $array = json_decode($json_data,true);  // 转换成数组
// echo '<pre>';
// print_r($array);

// 发送短信
$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
 //var_dump($data);
$json_data = send($ch,$data);
echo $json_data; //平台回调数据格式（json字符串）
exit;

// {code: 0, msg: "发送成功", count: 1, fee: 0.05, unit: "RMB", mobile: "13244988807", sid: 13710369442}

$array = json_decode($json_data,true);

// 发送模板短信
// 需要对value进行编码
$data=array('tpl_id'=>'1','tpl_value'=>('#code#').'='.urlencode('1234').'&'.urlencode('#company#').'='.urlencode('欢乐行'),'apikey'=>$apikey,'mobile'=>$mobile);
print_r ($data);
$json_data = tpl_send($ch,$data);
$array = json_decode($json_data,true);
echo '<pre>';
print_r($array);

// 发送语音验证码
$data=array('code'=>'9876','apikey'=>$apikey,'mobile'=>$mobile);
$json_data =voice_send($ch,$data);
$array = json_decode($json_data,true);
echo '<pre>';
print_r($array);

curl_close($ch);

/******************************************************************/
//获得账户
function get_user($ch,$apikey){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
    return curl_exec($ch);
}
function send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
}
function tpl_send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
}
function voice_send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
}

?>
?>